package com.linpq.road.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.linpq.common.exception.BusinessException;
import com.linpq.road.domain.RdHighSpeedRoad;
import com.linpq.road.domain.RdManagementUnit;
import com.linpq.road.domain.Roadsection;
import com.linpq.road.service.RdHighSpeedRoadService;
import com.linpq.road.service.RdManagementUnitService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.linpq.common.annotation.Log;
import com.linpq.common.enums.BusinessType;
import com.linpq.road.domain.RdRoadSection;
import com.linpq.road.service.RdRoadSectionService;
import com.linpq.common.core.controller.BaseController;
import com.linpq.common.core.domain.AjaxResult;
import com.linpq.common.utils.poi.ExcelUtil;
import com.linpq.common.core.page.TableDataInfo;

/**
 * 路段Controller
 *
 * @author wunh
 * @date 2021-07-29
 */
@Controller
@RequestMapping("/road/road_section")
public class RdRoadSectionController extends BaseController {
    private String prefix = "road/road_section";

    @Autowired
    private RdRoadSectionService rdRoadSectionService;
    @Autowired
    private RdHighSpeedRoadService rdHighSpeedRoadService;
    @Autowired
    private RdManagementUnitService rdManagementUnitService;
    private static final String ZERO="0";


    @RequiresPermissions("road:road_section:view")
    @GetMapping()
    public String road_section() {
        return prefix + "/road_section";
    }

    /**
     * 查询路段列表
     */
    @RequiresPermissions("road:road_section:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RdRoadSection rdRoadSection) {
        startPage();
        List<RdRoadSection> list = rdRoadSectionService.selectRdRoadSectionList(rdRoadSection);
        return getDataTable(list);
    }

    /**
     * 返回级联下拉框所需内容
     */
    @PostMapping("/findRoadSectionList")
    @ResponseBody
    public List<RdRoadSection> findRoadSectionList(@RequestBody RdRoadSection rdRoadSection){
        List<RdRoadSection> roadsectionList = rdRoadSectionService.findRoadSectionList(rdRoadSection);
        return roadsectionList;
    }

    /**
     * 导出路段列表
     */
    @RequiresPermissions("road:road_section:export")
    @Log(title = "路段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RdRoadSection rdRoadSection) {
        List<RdRoadSection> list = rdRoadSectionService.selectRdRoadSectionList(rdRoadSection);
        ExcelUtil<RdRoadSection> util = new ExcelUtil<RdRoadSection>(RdRoadSection. class);
        return util.exportExcel(list, "road_section");
    }

    /**
     * 新增路段
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存路段
     */
    @RequiresPermissions("road:road_section:add")
    @Log(title = "路段", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RdRoadSection rdRoadSection) {
        return toAjax(rdRoadSectionService.insertRdRoadSection(rdRoadSection));
    }

    /**
     * 修改路段
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id,ModelMap mmap) {
        RdRoadSection rdRoadSection =rdRoadSectionService.selectRdRoadSectionById(id);
        List <RdHighSpeedRoad> highSpeedRoadsList=rdHighSpeedRoadService.findList(new RdHighSpeedRoad());
        List<RdManagementUnit> managementUnitList =rdManagementUnitService.findList(new RdManagementUnit());
        mmap.put("highSpeedRoadsList",highSpeedRoadsList);
        mmap.put("managementUnitList",managementUnitList);
        mmap.put("rdRoadSection", rdRoadSection);
        return prefix + "/edit";
    }

    /**
     * 修改保存路段
     */
    @RequiresPermissions("road:road_section:edit")
    @Log(title = "路段", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RdRoadSection rdRoadSection) {
        return toAjax(rdRoadSectionService.updateRdRoadSection(rdRoadSection));
    }

    /**
     * 删除路段
     */
    @RequiresPermissions("road:road_section:remove")
    @Log(title = "路段", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(rdRoadSectionService.deleteRdRoadSectionByIds(ids));
    }

    /**
     * 传入路段id，查询路段的经纬度
     * @param id
     * @return
     */
    @GetMapping("/displayMap/{id}")
    @ResponseBody
    public Map displayMap(@PathVariable(value = "id",required = false) String id){
        if (StringUtils.isNotBlank(id)&&!"".equals(id)){
            Map map = new HashMap();
            RdRoadSection rdRoadSection = rdRoadSectionService.selectRdRoadSectionById(id);
            String[] split = rdRoadSection.getRemark().split(",");
            if (split.length==4){
                map.put("startLongitude",split[0]);
                map.put("startLatitude",split[1]);
                map.put("endLongitude",split[2]);
                map.put("endLatitude",split[3]);
            }
            return map;
        }
        return null;
    }

    /**
     * 当前路段状态
     * 当传入多个路段已经初始化完成返回成功码code=0
     * 当传入多个路段已经未初始化完成返回失败码code=500
     * 当传入多个路段信息，包括初始化和未初始化的路段编号时返回警告码code=301
     * @param id 路段编号
     * @return
     */
    @RequestMapping(value = "/selectRoadSectionStateById/{sectionid}",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult selectRoadSectionStateById(@PathVariable(value = "sectionid")String id){
        if (StringUtils.isNotBlank(id)&&!"".equals(id)){
            ArrayList<RdRoadSection> rdRoadSectionArrayList = new ArrayList<>();
            String[] split = id.split(",");
            for (int i = 0; i < split.length; i++) {
                RdRoadSection rdRoadSection = rdRoadSectionService.selectRdRoadSectionById(split[i]);
                //如果于0匹配则说明未初始化，加入集合
                if (ZERO.equals(rdRoadSection.getLocked())){
                    rdRoadSectionArrayList.add(rdRoadSection);
                }
            }
            //如果集合长度于传入的id数量相同则说明都未初始化，至少传入一个
            if (rdRoadSectionArrayList.size()==split.length){
                return AjaxResult.success();
            }
            if (rdRoadSectionArrayList.size()==0){
                return AjaxResult.error();
            }
            if (rdRoadSectionArrayList.size()!=split.length){
                return AjaxResult.warn("重新选择需要初始化的路径");
            }
        }
        return null;
    }
    /**
     * 路桩初始化完毕以后修改路桩初始化状态
     * @param id 路桩的编号
     * @return
     */
    @RequestMapping(value = "/modifyRoadSectionState/{sectionid}",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult modifyRoadSectionState(@PathVariable(value = "sectionid") String id) {
        if (com.linpq.common.utils.StringUtils.isNull(id)&&"".equals(id)){
            throw new BusinessException("路段编号异常");
        }
        AjaxResult ajaxResult = null;
        try {
            ajaxResult = rdRoadSectionService.modifyRoadSectionState(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return ajaxResult;
    }
}
